package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.Image;
import com.zzxz.practicaltrainingsys.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    // 上传头像
    @PostMapping("/upload")
    public ResponseEntity<String> uploadAvatar(
            @RequestParam String role,
            @RequestParam Integer userId,
            @RequestParam("file") MultipartFile file) {
        try {
            String imagePath = imageService.uploadAvatar(role, userId, file);
            return ResponseEntity.ok(imagePath);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("头像上传失败: " + e.getMessage());
        }
    }

    // 获取头像路径
    @GetMapping("/path")
    public ResponseEntity<String> getAvatarPath(
            @RequestParam String role,
            @RequestParam Integer userId) {
        try {
            Image image = new Image();
            image.setRole(role);
            image.setUserId(userId);
            String imagePath = imageService.getImagePath(image);
            if (imagePath == null) {
                return ResponseEntity.status(200).body("默认头像");
            }
            return ResponseEntity.ok(imagePath);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("获取头像路径失败: " + e.getMessage());
        }
    }
}